<!--用于分页的类-->
<?php
/**
 * Created by PhpStorm.
 * User: LYD
 * Date: 2019/2/15
 * Time: 21:30
 */
    class Page{
        private $total; //保存数据的总条数
        private $pagesize; //每页显示几条
        private $limit; //保存的是sql语句里面的limit
        private $page; //保存当前的页码
        private $pagenum; //保存当前可显示的最大页码
        private $url; //保存地址

        public function __construct($total,$pagesize)
        {
            $this->total = $total; //把外面获取到的数据走条数，保存在字段里
            $this->pagesize = $pagesize;
            $this->pagenum = ceil($total / $pagesize); //ceil余数进1，如4.5=5
            $this->page = $this->setpage(); //获取当前页码
            $this->limit = "limit ".($this->page-1)*$this->pagesize.",".$this->pagesize; //使数据从第几个开始，显示几个
            $this->url = $this->seturl();
        }

        //首页
        private function first(){
            return '<a href="'.$this->url.'?page=1">首页</a>';
        }

        //上一页
        private function prev(){
            if($this->page == 1){
                return "<span class='pageSpan'>上页</span>";
            }
            return '<a href="'.$this->url.'?page='.($this->page-1).'">上页</a>';
        }

        //下一页
        private function next(){
            if($this->page == $this->pagenum){
                return "<span class='pageSpan'>下页</span>";
            }
            return '<a href="'.$this->url.'?page='.($this->page+1).'">下页</a>';
        }

        //尾页
        private function last(){
            return '<a href="'.$this->url.'?page='.$this->pagenum.'">尾页</a>';
        }

        //数字目录
        private function pagelist(){
            $_pagelist ="";
            for($i = 1; $i <= $this->pagenum; $i++){
                if($i == 1){
                    $_pagelist .= '<a class=\'pagenumbers\' page="'.$i.'" href="'.$this->url.'?page='.$i.'">'.$i.'</a>';
                }else{
                    $_pagelist .= '<a class=\'pagenumbers\' page="'.$i.'" href="'.$this->url.'?page='.$i.'">'.$i.'</a>';
                }
            }

            return $_pagelist;
        }



        //动态获取地址
        private function seturl(){
            $_url = $_SERVER["REQUEST_URI"]; //获取当前页面的url地址
            $_par = parse_url($_url); //把url分成一个数组，以？号为界
            if(isset($_par["path"])){
                return $_par["path"];
            }
        }

        //获取,限制当前页码
        private function setPage(){
            if(!empty($_GET["page"])){
                if($_GET["page"] > 0){
                    if($_GET["page"] > $this->pagenum){
                        return $this->pagenum;
                    }else{
                        return $_GET["page"];
                    }
                }else{
                    return 1;
                }
            }else{
                return 1;
            }
        }

        //拦截器，可以使外部访问本类的私有字段
        public function __get($name)
        {
            return $this->$name;
        }

        //对外方法，首页，下页分页信息
        public function showpage(){
            $PageText = "";
            $PageText .= $this->first();
            $PageText .= $this->prev();
            return $PageText;
        }
        //对外方法，数字目录分页信息
        public function showpagelist(){
            $showlist = "";
            $showlist .= $this->pagelist();
            return $showlist;
        }
        //对外方法，尾页，下页分页信息
        public function showlast(){
            $showlast = "";
            $showlast .= $this->next();
            $showlast .= $this->last();
            return $showlast;
        }
    }
?>